<?php
/**
 * Created by PhpStorm.
 * User: cony
 * Date: 14-3-3
 * Time: 下午2:52
 */
namespace Admin\Model;
use Think\Model;
class ProductModel extends Model {

    public function listProduct($firstRow = 0, $listRows = 20,$map) {
        $M = M("Product");
        $list = $M->field("`id`,`title`,`status`,`published`,`cid`,`aid`,`is_recommend`,`lang`,`wap_display`,`year`,`brand`,`need`,`t_xq`,`t_gj`,`t_lx`,`t_hy`,`t_sj`,`man`")->where($map)->order("`published` DESC")->limit("$firstRow , $listRows")->select();
        $statusArr = array("待审核", "已发布");
        $aidArr = M("Admin")->field("`aid`,`email`,`nickname`")->select();
        foreach ($aidArr as $k => $v) {
            $aids[$v['aid']] = $v;
        }
        unset($aidArr);
        $map['type']='p';
        $cidArr = M("Category")->field("`cid`,`name`")->where($map)->select();
        foreach ($cidArr as $k => $v) {
            $cids[$v['cid']] = $v;
        }
        unset($cidArr);
        foreach ($list as $k => $v) {
            $list[$k]['aidName'] =$aids[$v['aid']]['nickname'] == '' ? $aids[$v['aid']]['email'] : $aids[$v['aid']]['nickname'];
//             $list[$k]['status'] = $statusArr[$v['status']];
            $list[$k]['t_lx'] = $cids[$v['t_lx']]['name'];
        }
        return $list;
    }

    public function category() {
        if (IS_POST) {
            $act = $_POST[act];
            $data = $_POST['data'];
            $data['name'] = addslashes($data['name']);
            $data['type'] ='p';
            $M = M("Category");
            if ($act == "add") { //添加分类
                unset($data[cid]);
                $data['type']= $_POST['type'];
                if ($M->where($data)->count() == 0) {
                    return ($M->add($data)) ? array('status' => 1, 
                        'info' => '分类 ' . $data['name'] . ' 已经成功添加到系统中', 
                        'url' => U('Product/category', array('time' => time()))) : array('status' => 0, 'info' => '分类 ' . $data['name'] . ' 添加失败');
                } else {
                    return array('status' => 0, 'info' => '系统中已经存在分类' . $data['name']);
                }
            } else if ($act == "edit") { //修改分类
                if (empty($data['name'])) {
                    unset($data['name']);
                }
                if ($data['pid'] == $data['cid']) {
                    unset($data['pid']);
                }
                return ($M->save($data)) ? array('status' => 1, 
                    'info' => '分类 ' . $data['name'] . ' 已经成功更新', 
                    'url' => U('Product/category', array('time' => time()))) : array('status' => 0, 'info' => '分类 ' . $data['name'] . ' 更新失败');
            } else if ($act == "del") { //删除分类
                unset($data['pid'], $data['name']);
                if($M->where('pid='.$data['cid'].' AND cid!='.$data['cid'])->count()>0){
                    return (array('status' => 0, 'info' => $data['name'] . '存在下级分类，请先删除'));
                    exit;
                }
                return ($M->where($data)->delete()) ? array('status' => 1, 
                    'info' => '分类 ' . $data['name'] . ' 已经成功删除', 
                    'url' => U('Product/category', array('time' => time()))) : array('status' => 0, 'info' => '分类 ' . $data['name'] . ' 删除失败');
            }
        } else {
            $map['type']='p';
            $cat = new \Org\Util\Category('Category', array('cid', 'pid', 'name', 'fullname'),$map);
            return $cat->getList();//获取分类结构
        }
    }

    public function addProduct($imagename='') {
        $M = M("Product");
        $data = $_POST['info'];
        $data['published'] = time();
        $data['summary']=nl2br($data['summary']);
        $data['title']=strip_tags($data['title']);
        $data['keywords'] = str_replace('，',',',$data['keywords']);
        $data['t_gj'] = M('Category')->where(['name'=>$data['t_gj']])->getField('cid');
        // $data['media_id']=I('post.vidoe');
        // var_dump($data['media_id']);

        $image=I('post.image_1');
        if($image){
            if(is_array($image)){
                $M_image = M("images");
                $image_id=array();
                foreach($image as $k=>$v){
                    $img_data['savepath']=$v;
                    $img_data['savename']=end(explode('/',$v));
                    $img_data['create_time']=time();
                    $img_data['catname']='product';
                    if($v)
                    $image_id[$k]=$M_image->add($img_data);
                }
                $data['image_id']=implode(',',$image_id);
            }
        }
        $t_jx=I('post.t_jx');
        if($t_jx){
            if(is_array($t_jx)){
                $data['t_jx']=implode(',',$t_jx);
            }
        }
        $media=I('post.media');
        if($media){
            if(is_array($media)){
                $data['media_id']=implode(',',$media);
            }
        }
        $data['aid'] = $_SESSION['my_info']['aid'];
        if(empty($data['title'])){
            return array('status' => 0, 'info' => "请输入标题！",'url'=>__SELF__);
        }
        if (empty($data['summary'])) {
            // $data['summary'] = cutStr(strip_tags($data['content']), 200);
        }
        if ($M->add($data)) {
            return array('status' => 1, 'info' => "已经发布", 'url' => U('Product/index'));
        } else {
            return array('status' => 0, 'info' => "发布失败，请刷新页面尝试操作");
        }
    }

    public function edit() {
        $M = M("Product");
        $data = $_POST['info'];
        $data['update_time'] = time();
        $data['summary']=$data['summary'];
        $data['title']=strip_tags($data['title']);
        $data['keywords'] = str_replace('，',',',$data['keywords']);
        $data['t_gj'] = M('Category')->where(['name'=>$data['t_gj']])->getField('cid');
        
        $M_image = M("images");
        $map['id']=$data['id'];
        $image_ids=$M->where($map)->getField('image_id');
        $image_map['id']=array('in',$image_ids);
        $M_image->where($image_map)->delete();
        $data['image_id']='';

        $image=I('post.image_1');
        if($image){
            if(is_array($image)){
                $image_id=array();
                foreach($image as $k=>$v){
                    $img_data['savepath']=$v;
                    $img_data['savename']=end(explode('/',$v));
                    $img_data['create_time']=time();
                    $img_data['catname']='product';
                    if($v)
                    $image_id[$k]=$M_image->add($img_data);
                }
                $data['image_id']=implode(',',$image_id);
            }
        }
        $media=I('post.media');
        if($media){
            if(is_array($media)){
                $data['media_id']=implode(',',$media);
            }
        }

        $t_jx=I('post.t_jx');
        if($t_jx){
            if(is_array($t_jx)){
                for ($i=0; $i < 10; $i++) { 
                    $key = array_search("5", $t_jx);
                    if ($key !== false){
                        array_splice($t_jx,$key,1);
                    }else{
                        break;
                    }
                }
                 $data['t_jx']=implode(',',$t_jx);
            }
        }

        if(empty($data['title'])){
            return array('status' => 0, 'info' => "请输入标题！",'url'=>__SELF__);
        }
        
        
        $data = $this->checkUpdate($data);
        if ($M->save($data)) {
            return array('status' => 1, 'info' => "已经更新", 'url' => U('Product/index'));
        } else {
            return array('status' => 0, 'info' => "更新失败，请刷新页面尝试操作");
        }
    }
    
    /**
     * 检查‘洞察’或者‘类型’是否有修改到，如果有就要记录修改次数
     * @param unknown $data
     * date:2017年4月6日
     * author: EK_熊
     */
    public function checkUpdate($data){
        $proInfo = $this->where(['id'=>$data['id']])->field('description,content,update_num')->find();
        if($data['description'] !== $proInfo['description'] || $data['content'] !== $proInfo['content']) {
            $data['update_num'] = $proInfo['update_num']+1;
            return $data;
        }
        return $data;
    }

}